<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Storage;

class UploaderController extends Controller
{
    /**
     * 七牛上传文件的处理
     */
    public function qiniu(Request $request)
    {
        //判断文件上传是否成功
        if($request->hasFile('file') && $request->file('file')->isValid()){
                //有文件上传
                $filename = sha1(time().$request->file('file')->getClientOriginalName()).
                '.'.$request->file('file')->getClientOriginalExtension();
                //文件保存/移动
                Storage::disk('qiniu')->put($filename, file_get_contents($request->file('file')->path()));

                //返回数据
            $result = [
                'errCode' => '0',
                'errMsg'  => '',
                'sucMsg'  => '文件上传成功',
                'path' =>  Storage::disk('qiniu')->getAdapter()->downloadUrl($filename)
            ];
        }else{
            $result = [
                'errCode' => '000001',
                'errMsg'  =>  $request->file('file')->getErrorMessage()
            ];
        }
        return response()->json($result);
    }
    /**
     * 上传文件的处理
     */
    public function webuploader(Request $request)
    {
        //判断文件上传是否成功
        if($request->hasFile('file') && $request->file('file')->isValid()){
            //有文件上传
            $filename = sha1(time().$request->file('file')->getClientOriginalName()).
                '.'.$request->file('file')->getClientOriginalExtension();
            //文件保存/移动
            Storage::disk('public')->put($filename, file_get_contents($request->file('file')->path()));

            //返回数据
            $result = [
                'errCode' => '0',
                'errMsg'  => '',
                'sucMsg'  => '文件上传成功',
                'path' =>  'storage/'.$filename
            ];
        }else{
            $result = [
                'errCode' => '000001',
                'errMsg'  =>  $request->file('file')->getErrorMessage()
            ];
        }
        return response()->json($result);
    }
}
